<?php
 require_once ("include.php");
 require_once ("registereduserobj.php");
 
 if (isset($_REQUEST['redopassword'])) {
  echo resetpassword();
 } elseif (authenticate(false) !== '') {
   echo showscreen();
 } else {
  echo alreadyon();
 }

?>

<?php
 function alreadyon() {
 $o = "You are already logged on ";
 return $o;
 }
?>
<?php
function showscreen() {
list($fname,$cap) = createcaptcha();
$o = "
<link href='css/style.css' rel='stylesheet' type='text/css' />
<div class='banner'>
</div>
<form method=post>
<table class='logon'>
<tr><td>Email address</td><td><input type=text name=userid></td></tr>
<tr><td>Please type the 6 digit number</td><td><input type=text name=captcha></td></tr>
<tr><td><input type=submit name=redopassword value='Reset my password'></td></tr>
</table>
</form>
<img src=$fname>";
return $o;
}
?>

<?php
 function createcaptcha() {
$im = imagecreatetruecolor(60, 20);
$cap = rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9).rand(1,9) ;
$font = 'arial.ttf';
for ($i=0;$i<strlen($cap);$i++) {
 $text_color = imagecolorallocate($im, rand(200,255),rand(100,255),rand(100,255));
 $bg_color = imagecolorallocate($im,0,0,0);
 imagettftext($im, 8, rand(-30,30), 5+8*$i, 10+rand(1,6), $text_color, $font, $cap[$i]);  
}
$fname = 'scrap/f'.time().'.jpg';
imagejpeg($im, "$fname",100);
imagedestroy($im);
$_SESSION['captcha']= $cap;
$arr = array($fname,$cap);
return $arr;
}
?> 

<?php
 function resetpassword() {
  $email = @$_REQUEST['userid'];
	$cap = @$_REQUEST['captcha'];
	$sesscap = @$_SESSION['captcha'];
	if ($sesscap <> '' && $sesscap == $cap) {
   if (filter_var($email,FILTER_VALIDATE_EMAIL)) {
	  $obj = new registereduser();
	  if ($obj->readuser($email)) { ;
	   $confirmcode = md5($obj->header[0].'|'.$email.time());
	   $obj->header[7] = $confirmcode ;
     $obj->writedb();
 	   $message = "You requested a password reminder\n";
     $message .= "http://liftmate.com/resetpassword.php?id=".$confirmcode."&u=".$email;
	   $email = "antonylawler@gmail.com"; // Test purposes only
     mail($email,'Password reset instructions',$message);
	   $o = "A new link has been sent. Please follow the link to reset your password.";	
	  } else {
	   $o = "You do not have an account. Would you like to create one ?";
	  }
	 } else {
	  $o = "That is not a valid email address";
	 }
	} else {
	 $o = "That is the wrong captcha. <a href=forgotpassword.php>Try again</a>";
	}
	return $o;
  	
 }
?>

